﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Popups;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;

namespace GSBA.Common
{
    public class NavigationService
    {
        private Frame mainFrame;

        private static NavigationService instance;

        public static void Instantiate(Frame rootFrame)
        {
            if (instance == null)
            {
                instance = new NavigationService(rootFrame);
            }
        }

        public static NavigationService GetInstance()
        {
            return instance;
        }

        public NavigationService(Frame mainFrame)
        {
            this.mainFrame = mainFrame;
        }

        public void Navigate(Type type)
        {
            this.Navigate(type, null);
        }

        public void Navigate(Type type, object parameters)
        {
            this.mainFrame.Navigate(type, parameters);
        }

        public async void NavigateUri(string uri)
        {
            if (!uri.StartsWith("http://"))
            {
                uri = "http://" + uri;
            }
            await Windows.System.Launcher.LaunchUriAsync(new Uri(uri));
        }

        public async void MakeCall(string number)
        {
            if (!number.StartsWith("+"))
            {
                number = "+1" + number;
            }

            
            // This is a really ugly way of doing message dialog, but I'm rushing to complete this, so this is the easy way out
            var messageDialog = new MessageDialog("Do you want to call " + number+" with Skype?", "Call " + number);

            messageDialog.Commands.Add(new UICommand("Cancel", (command) =>
            {
            }));

            messageDialog.Commands.Add(new UICommand("Call", (command) =>
            {
                Windows.System.Launcher.LaunchUriAsync(new Uri("skype:" + number + "?call"));
            }));

            messageDialog.DefaultCommandIndex = 1;

            // Show the message dialog
            await messageDialog.ShowAsync();
        }

        public async void SendMail(string email)
        {
            await Windows.System.Launcher.LaunchUriAsync(new Uri("mailto:" + email));
        }

        public void Navigate(string viewName, object parameters)
        {
            var assembly = typeof(Views.App).GetTypeInfo().Assembly;
            var viewNetType = assembly.GetType(typeof(Views.App).Namespace + '.' + viewName);

            if (viewNetType != null)
            {
                this.Navigate(viewNetType, parameters);
            }
        }

        public void Navigate(string viewName)
        {
            this.Navigate(viewName, null);
        }

        public void GoBack()
        {
            if (this.mainFrame.CanGoBack)
            {
                this.mainFrame.GoBack();
            }
        }
    }
}
